#!/bin/bash
#QT_DIR=/home/sumscope/Qt
QT_DIR=/opt/Qt/5.15.2/gcc_64
export PATH=${QT_DIR}/bin:$PATH
export LIB_PATH=${QT_DIR}/lib:$LIB_PATH
export PLUGIN_PATH=${QT_DIR}/plugins:$PLUGIN_PATH
export QML2_PATH=${QT_DIR}/qml:$QML2_PATH
export LD_LIBRARY_PATH=${QT_DIR}/lib:$LD_LIBRARY_PATH
echo $PATH
echo $LIB_PATH
echo $PLUGIN_PATH
echo $QML2_PATH
echo $LD_LIBRARY_PATH


root_dir=`pwd`
cd $root_dir


rm -rf pack
mkdir pack
#0. create default dirs
cd $root_dir/pack
mkdir -p usr/bin usr/lib usr/share/applications usr/share/icons/hicolor/scalable/apps

BUILD_DIR=$root_dir/../output/Linux/Release
#BUILD_DIR=$root_dir/../output/Linux/Debug

QCEFVIEW_DIR=$root_dir/../3rdParty/QCefView/

#1 qeubee
cp ${BUILD_DIR}/qeubee $root_dir/pack/usr/bin/
cp -r ${BUILD_DIR}/config $root_dir/pack/usr/bin/
rm $root_dir/pack/usr/bin/config/modules.cfg
mv $root_dir/pack/usr/bin/config/modules_linux_release.cfg  $root_dir/pack/usr/bin/config/modules.cfg

cp $root_dir/crash_reporter.cfg $root_dir/pack/usr/bin/

mkdir -p $root_dir/pack/usr/bin/qss
mkdir -p $root_dir/pack/usr/bin/res
cp $root_dir/../source/widgets/res/*.qss $root_dir/pack/usr/bin/qss/
cd $root_dir/../source/widgets/res/svg/ && make clean all && cd -
cp $root_dir/../source/widgets/res/svg/output/*.svg $root_dir/pack/usr/bin/res/

#cp ${BUILD_DIR}/libqbbond.so $root_dir/pack/usr/lib/
#cp ${BUILD_DIR}/libqbfixedincome.so $root_dir/pack/usr/lib/
cp ${BUILD_DIR}/*.so $root_dir/pack/usr/lib/
#cp ${QT_DIR}/libexec/QtWebEngineProcess $root_dir/pack/usr/bin/

#2. resources
cp $root_dir/com.sumscope.qeubee.svg $root_dir/pack/usr/share/icons/hicolor/scalable/apps/
cp $root_dir/com.sumscope.qeubee.desktop $root_dir/pack/usr/share/applications/

#cd ${QCEFVIEW_DIR}/out && find . -type f -name libcef.so | xargs strip && cd -

cp -r ${QCEFVIEW_DIR}/out/linux.x86_64/QCefView/bin $root_dir/pack/usr/cef
cd $root_dir/pack/usr/cef && chmod +x CefViewWing *.so *.so.* chrome-sandbox && cd -

export LD_LIBRARY_PATH=$root_dir/pack/usr/lib:$root_dir/pack/usr/cef:$LD_LIBRARY_PATH

cd $root_dir/
generatesyms() {
  txt=`whereis dump_syms`
  dump_syms_path=${txt:11}
  
  if [ -z "$dump_syms_path" ]; then
    echo "未找到dump_syms，无法完成symbol导出"
    return 1
  fi 
  echo "Using dump_sysm from:" $dump_syms_path
  cp ${dump_syms_path} $root_dir/pack/

  python3 ./generate_breakpad_symbols.py --build-dir=$root_dir/pack/ --symbols-dir=./qeubee-x-syms --binary=$root_dir/pack/usr/bin/qeubee --clear -v --platform=linux
  python3 ./generate_breakpad_symbols.py --build-dir=$root_dir/pack/ --symbols-dir=./qeubee-x-syms --binary=$root_dir/pack/usr/lib/libqbbond.so -v --platform=linux
  python3 ./generate_breakpad_symbols.py --build-dir=$root_dir/pack/ --symbols-dir=./qeubee-x-syms --binary=$root_dir/pack/usr/lib/libqbfixedincome.so -v --platform=linux
  rm -f $root_dir/pack/dump_syms
  find $root_dir/pack/usr/lib/ -name lib*.so | xargs strip
  strip $root_dir/pack/usr/cef/libQCefView.so
  strip $root_dir/pack/usr/cef/libcef.so
  strip $root_dir/pack/usr/bin/qeubee
  return 0
}

echo "++++++++++++++++++++++++++++++++++++++++++++++++++"
generatesyms
echo "++++++++++++++++++++++++++++++++++++++++++++++++++"

# -executable=$root_dir/pack/usr/bin/QtWebEngineProcess
# 将 libnss3.so 加入软件包会导致使用QWebEngine不明原因的网页白屏 ！！！
# 但是使用QCefView不会 ！！！

linuxdeployqt $root_dir/pack/usr/share/applications/com.sumscope.qeubee.desktop -exclude-libs=libcef.so -qmake=${QT_DIR}/bin/qmake -appimage

if [ $? -ne 0 ]; then
  exit -1
fi

cd $root_dir

#exit 0

echo "===============================    making dep    ==============================="
rm -rf deb_pack
mkdir deb_pack
cd deb_pack
tar -zxvf $root_dir/aarch64_deb_files.tar.gz ./
arch=`uname -m`
if [ "$arch" = "x86_64" ]; then 
  sed -i 's/arm64/amd64/g' $root_dir/deb_pack/DEBIAN/control
  sed -i 's/arm64/amd64/g' $root_dir/deb_pack/opt/apps/com.sumscope.qeubee/info
fi

cp -r $root_dir/pack/usr/bin $root_dir/deb_pack/opt/apps/com.sumscope.qeubee/files/
cp -r $root_dir/pack/usr/lib $root_dir/deb_pack/opt/apps/com.sumscope.qeubee/files/
cp -r $root_dir/pack/usr/plugins $root_dir/deb_pack/opt/apps/com.sumscope.qeubee/files/
cp -r $root_dir/pack/usr/cef $root_dir/deb_pack/opt/apps/com.sumscope.qeubee/files/
cp -r $root_dir/pack/usr/translations $root_dir/deb_pack/opt/apps/com.sumscope.qeubee/files/

dpkg-deb --root-owner-group -b ./ ../com.sumscope.qeubee.deb


